﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HQPad.Security;
using HQPad.Models;
using HQPad.DAL;
using HQPad.Services;
using HQPad.Helpers;

namespace HQPad.Controllers
{
    public class HomeController : BaseController
    {
        [AllowAnonymous]
        public ActionResult Index()
        {
            BuildPageTitle("Welcome to the HQ Character Pad");

            return View();
        }

        [AllowAnonymous]
        public ActionResult About()
        {
            BuildPageTitle("About the HQ Character Pad");

            return View();
        }

        public ActionResult Dashboard()
        {
            BuildPageTitle("Your HQ Pad Character Dashboard");

            string userName = AuthenticationHelper.GetCurrentUserName(HttpContext);
            string sessionId = AuthenticationHelper.GetCurrentSessionId(Request);

            // Build the dashboard model of users characters and scratch characters.
            IUnitOfWork characterContext = new CharacterContext();
            ICharacterService characterService = new CharacterService(characterContext);

            DashboardModel model = new DashboardModel()
            {
                Characters = characterService.ListCharacters(userName),
                ScratchPadCharacters = characterService.ListScratchPadCharacters(userName, sessionId)
            };

            return View(model);
        }

        public ActionResult Load(int id)
        {
            string userName = AuthenticationHelper.GetCurrentUserName(HttpContext);
            string sessionId = AuthenticationHelper.GetCurrentSessionId(Request);

            // Build the dashboard model of users characters and scratch characters.
            //IUnitOfWork context = new HQContext();
            IUnitOfWork characterContext = new CharacterContext();
            ICharacterService characterService = new CharacterService(characterContext);

            //// Get character by (name) slug.
            //Character character = characterService.GetCharacter(id);

            // Copy to scratch pad
            characterService.CopyCharacterToScratchPad(id, sessionId);

            characterContext.Save();

            BuildPageTitle("Your HQ Pad Character Dashboard");

            DashboardModel model = new DashboardModel()
            {
                Characters = characterService.ListCharacters(userName),
                ScratchPadCharacters = characterService.ListScratchPadCharacters(userName, sessionId)
            };

            return View("Dashboard", model);
        }

        [AllowAnonymous]
        public ActionResult BetaWarning()
        {
            return View();
        }

        public ActionResult NotFound() // web.config sends 404s here
        {
            return View();
        }

    }

    public class DashboardModel
    {
        public IEnumerable<ScratchPad> ScratchPadCharacters { get; set; }
        public IEnumerable<Character> Characters { get; set; }
    }
}
